

//
//  ZHomeSearchSubVC.m
//  ZQLearn
//
//  Created by 朵 on 2019/5/14.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "ZHomeSearchSubVC.h"
#import "SearchBar.h"
#import "ReadModel.h"
#import "OrderCell.h"
#import "HomeModel.h"
#import "ZBookMsgDetailVC.h"
#import "VideoCell.h"
#import "ZVideoViewController.h"
@interface ZHomeSearchSubVC ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)SearchBar *searchBar;



@property (nonatomic,copy)NSString *index;

@end

#define cellIdentifier @"order"
#define cellId @"video"

@implementation ZHomeSearchSubVC
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight-ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.rowHeight = 105.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"OrderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
         [_tableView registerNib:[UINib nibWithNibName:@"VideoCell" bundle:nil] forCellReuseIdentifier:cellId];
        WeakStament(ws);
        //        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
        //            [ws refreshData];
        //        }];
        
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
//    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 45, 45) title:@"搜索" Image:@"" selectedIMG:@"" tartget:self action:@selector(sousuoAction)];
    
    self.dataArray = [NSMutableArray array];
    
    [self.view addSubview:self.tableView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sousuoAction) name:@"sou" object:nil];
    
//    [self addSearchBar];
    // Do any additional setup after loading the view.
}


- (void)loadData{
    
    YYLog(@"8888%@",self.searchBar.textFd.text);
    
    
    NSDictionary *dic = @{@"name":[UserModel shareInstance].txtContent?:@"",@"pageIndex":@"1",@"pageSize":@"100",};
    
    
    
    NSString *url;
    if ([self.index isEqualToString:@"0"]) {
        url = kHomeSearchBookUrl;
    }else{
        url = kHomeSearchVideoUrl;
    }
    
    [self.dataArray removeAllObjects];
    [ZNetworkService getRequestWithBaseUrl:url setParameters:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
           
            NSArray *arr = result[@"result"];
            if (arr.count>0) {
                for (NSDictionary *dic in arr) {
                    ReadModel *model = [ReadModel mj_objectWithKeyValues:dic];
                    [self.dataArray addObject:model];
                }
                [self.tableView reloadData];
               
            }else{
                
                [AFToast showText:@"当前搜索内容为空"];
                [self.tableView reloadData];
            }
             [self.view endEditing:YES];
            
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
}

#pragma mark - 搜索
///搜索按钮
- (void)sousuoAction{
    ///搜索接口请求
    [self loadData];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.index isEqualToString:@"0"]) {
        return 115.f;
    }else{
        return 185.f;
    }
}

#pragma mark - datasource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    if ([self.index isEqualToString:@"0"]) {
        
    
    ReadModel *model = self.dataArray[indexPath.row];
    OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.numLab.hidden = YES;
    cell.jifenLab.textColor = kDarkGrayColor;
    cell.nameLab.text = model.name;
    cell.jifenLab.text = model.content;
    cell.topHeight.constant = 20.f;
    cell.bottomHeight.constant = 30.f;
    //    cell.numLab.text =[NSString stringWithFormat:@"X%@",subModel.commodityNumber];
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo]] placeholderImage:nil];
    return cell;
    }else{
        ReadModel *model = self.dataArray[indexPath.row];
        
        VideoCell *cell= [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
        cell.bottomBar.hidden = YES;
        cell.titleLab.text = model.name;
        cell.bottomHeight.constant = 30.f;
        cell.leftMargin.constant = 15.f;
        cell.rightMargin.constant = 15.f;
        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo]] placeholderImage:nil];
        //    cell.titleLab.text = model.title;
        //    cell.timeLab.text = model.updateTime;
        return cell;
    }
    return nil;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.index isEqualToString:@"0"]) {
        ReadModel *hmodel = self.dataArray[indexPath.row];
        ZBookMsgDetailVC *det = [ZBookMsgDetailVC new];
        MidModel *model = [MidModel new];
        model.name = hmodel.name;
        model.picUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,hmodel.photo];
        
        det.MModel = model;
        [self.navigationController pushViewController:det animated:YES];
    }else{
        //视频详情
        ReadModel *model = self.dataArray[indexPath.row];
        MidModel *mModel = [MidModel new];
        mModel.name = model.name;
        mModel.ID = model.ID;
        mModel.picUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo];
        mModel.price = model.price;
        NSString *vUrl = model.videoUrl;
        mModel.videoUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,vUrl];
        YYLog(@"%@",model.videoUrl);
        ZVideoViewController *videoDetail = [ZVideoViewController new];
        videoDetail.MModel = mModel;
        [self.navigationController pushViewController:videoDetail animated:YES];
    }
    
}

#pragma mark ZJScrollPageViewChildVcDelegate
- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    
    YYLog(@"%ld",index);
//    [UserModel shareInstance].tabIndex = index;
//    self.currentIndex = index;
    
}
- (void)zj_viewDidAppearForIndex:(NSInteger)index{
    
}
- (void)zj_viewWillDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index{
    //这里具体处理请求逻辑不同的页面根据索引进行请求
    YYLog(@"%ld",index);
    self.index = [NSString stringWithFormat:@"%ld",index];
//    self.currentIndex = index;
//    //        [self loadDataWithIndex:index];
//    if (self.datas.count>0) {
//
//        NewsModel *model = self.datas[index];
//        self.currentIndexId = model.ID;
//        [self loadDataWithId:model.ID];
//        YYLog(@"%@",model.ID);
//
//    }
    
}

@end
